from app.dao.base import BaseDao
from app.schema.chat import ChatInfoSchema, ChatDetailSchema, ChatRespDetailSchema

from ..model.chat import Chat
from ..model.message import Message
from ..model.ai import Ai
class ChatDao(BaseDao):
    Model = Chat
    
    def search_chat(self,ai_id:int,user_id:int):
        return self.db.sess.query(Chat).filter(
            Chat.ai_id == ai_id,
            Chat.user_id == user_id,
            Chat.is_deleted == 0
        ).first()
    
    def create_chat(self, chat_info: ChatInfoSchema):
        chat = Chat(
            ai_id=chat_info.ai_id,
            user_id=chat_info.user_id,
            group_id=chat_info.group_id,
        )
        self.db.sess.add(chat)
        self.db.sess.commit()
        return chat
    
    async def search_by_chat_id(self, chat_id: int):
        return await self.db.sess.query(Message).filter(
            Message.chat_id == chat_id,
            Message.is_deleted == 0
        ).first()
        